Dynomotion

Group: DynoMotion Message: 10668 From: cnc_machines Date: 12/12/2014
Subject: External Buttons

Greetings,


I am reviewing the "ExternalButtons.c" program and trying to understand how it is used. This needs to always be running in order to be checking the status of the buttons. Should this be copied and pasted into the "INIT" program to ensure that it is always running?


I would also like to tie my homing program to an external button. What is the best method to do this? I am not using all of the buttons in the external buttons program. Should I just copy and paste my homing program into the external buttons program? If someone pressed the homing button while the machine was doing coordinated motion - would I have to stop this before homing is executed?


Thanks,


Scott

Group: DynoMotion Message: 10669 From: Tom Kerekes Date: 12/12/2014
Subject: Re: External Buttons
Hi Scott,

Basically yes, but it is more a matter of merging the code from the External buttons or Homing program together.  Programs have a structure.   Each program needs to have a single "main()" function where code is placed and execution begins.  Curly brackets mark the beginning and end of the code in the function.  A good programmer indents the lines within the curly brackets to make it easy to visualize the code in the function block.  A program can contain other sub-functions.  Take some time to study a program to see if you can identify the structure and the parts.   

The general idea is to have an INIT program that first initializes everything then loops doing other operations forever after.  That might include checking buttons.

Are you using KMotionCNC?  Probably the simplest way to have an external Homing button would be to have a User Button in KMotionCNC that will run your Homing program (in another Thread).  The have the External Button send a Push User Button (ie     DoPCInt(PC_COMM_USER_BUTTON,3);

You obviously couldn't and wouldn't want to perform a homing sequence at the same time as a GCode Job was running.  You might ignore the Home Button while a Job is active using an:  if (!JOB_ACTIVE) condition in the C Program Home Button Code.

HTH
Regards
TK


Group: DynoMotion Message: 10684 From: cnc_machines Date: 12/17/2014
Subject: Re: External Buttons
Tom,

I believe that I understand the program structure. Thanks for your patience in explaining. I updated the example external buttons program to only use the Cycle Start command. My problem is that it doesn't loop forever. When I run the program it loops forever until the button is pressed. After I press the button and the cycle starts I have to restart the program to get the button to work again.

Here is the part of the program. Am I missing something? To me it looks like I should be able to repeatedly use the external button and the program should never stop? Do you have any suggestions on this?



Thanks,
Group: DynoMotion Message: 10686 From: Tom Kerekes Date: 12/17/2014
Subject: Re: External Buttons
Hi Scott,

Which Thread are you running this in?  Could it be that your GCode runs something else in this Thread overwriting it?  You can observe the Green Bars on the Threads in KMotion.exe | C Programs Screen to see which Threads are actually running in KFLOP.

Regards
TK

Group: DynoMotion Message: 10690 From: cnc_machines Date: 12/18/2014
Subject: Re: External Buttons
Tom,

Thanks, you were right. I was using the same thread in the G-Code program to start the spindle. Could I get a little clarification on how threads are used.

Thread 1 - INIT File
Thread 2 - Homing File
Thread 3 - Watch External Buttons

I have several other functions I would like to do. Spindle on (M3), spindle speed, change spindle direction. Could all of these be connected with thread 2? Typically when M3 is called the spindle speed is called immediately after. Should I be concerned that the controller will call both of these at the same time and cause a problem? If two custom M codes are called at the same time in a program - do they need to have separate threads?

Thanks,

Scott
Group: DynoMotion Message: 10692 From: Tom Kerekes Date: 12/18/2014
Subject: Re: External Buttons
Hi Scott,

If you specify the Exec/Wait option then the Interpreter will wait for the C Program to finish before doing anything else.  Even without Wait you should be able to use the same Thread if the Program only executes for microseconds as it would take milliseconds to begin downloading the next program.  However if the C Program does a wait for something (ie Spindle to spin up to speed) then a different Thread should be used.  It doesn't hurt anything to use different Threads unless you completely run out of them.

Eventually I think you should merge your Watch External Buttons into the INIT Program so only Threads 1 and 2 are used.  Each extra Thread that is running consumes DSP resources away from the KFLOP system Thread to perform USB communication and such so the number of Threads running should be minimized.

HTH
Regards
TK

Group: DynoMotion Message: 10715 From: cnc_machines Date: 12/23/2014
Subject: Re: External Buttons
Tom,

Thanks for the clarification. That is very helpful. Regarding merging the buttons program, and the init file. I would just need to merge all of the #Include files at the beginning, the mains in one location, and then the loop forever into one?

I was hoping to understand the syntax a bit better for executing a program from within another one. For example, in the external buttons program, it has an infinite loop looking for the state of each button. How would I call the homing program if the homing button was pressed? I would probably want to stop all motion and then execute the external button program. Is there a simple line of code to call the program?

Thanks,

Scott


---In DynoMotion@yahoogroups.com, <tk@...> wrote :

Hi Scott,

If you specify the Exec/Wait option then the Interpreter will wait for the C Program to finish before doing anything else.  Even without Wait you should be able to use the same Thread if the Program only executes for microseconds as it would take milliseconds to begin downloading the next program.  However if the C Program does a wait for something (ie Spindle to spin up to speed) then a different Thread should be used.  It doesn't hurt anything to use different Threads unless you completely run out of them.

Eventually I think you should merge your Watch External Buttons into the INIT Program so only Threads 1 and 2 are used.  Each extra Thread that is running consumes DSP resources away from the KFLOP system Thread to perform USB communication and such so the number of Threads running should be minimized.

HTH
Regards
TK

Group: DynoMotion Message: 10723 From: Tom Kerekes Date: 12/23/2014
Subject: Re: External Buttons
Hi Scott,

Correct but after merging two programs you should have one "main" function. 

Not sure what you mean by "executing a program from within another one".  A KFLOP program can not directly Compile/Download/Execute a program into another Thread.  KFLOP doesn't have access to the PC Disk or the Compiler that runs on the PC.  There is a mechanism to request KMotionCNC to push a User Button to do all this however.  See:     DoPCInt(PC_COMM_USER_BUTTON,3);  in the example: KFLOPtoPCCmdExamples.c  Configure KMotionCNC to Execute the Program in a Thread when a User Button is pushed.  You can even hide the User Button by not specifying a Label.  The External Home button will not work if KMotionCNC is not running on the PC.

Another approach may work to do your homing within the Init Program itself.  Just add your homing code as a function in the Init program and call it.  However in this case while the Homing is being called the other operations in the "forever loop" will not be performed.  For something like a Cycle Start button this might actually be an advantage.  For other buttons like EStop it may not be desirable.  There are software techniques to write things like homing in a non blocking manner (state machine driven), but that is probably more complicated than what you want to do.

If the other program is already compiled, downloaded, and permanently flashed into a Thread then it is possible to Launch it from another Program, but we don't recommend doing this as it become complicated keeping track of what is in KFLOP and changes and changing Versions or boards becomes complicated.

HTH
Regards
TK



Group: DynoMotion Message: 11576 From: cnc_machines Date: 5/26/2015
Subject: External Buttons
Greetings,

I am having some issues with my External buttons program.

I am trying to have it home when I press one physical button, and then run a second homing program when another physical button is pushed.

It works just fine with the first homing loop, but when I try to add the second one with "PC_comm_user_button, 2" it will not function. I am not sure what I am misunderstanding here. Any ideas?

        // Handle Home
        result = Debounce(ReadBit(Home),&fcount,&flast,&flastsolid);
        if  (result == 1)
        {
            DoPCInt(PC_COMM_USER_BUTTON,1);
            DoPCInt(PC_COMM_RESTART);
        }

        // Handle Home Blade
        result = Debounce(ReadBit(Blade),&fcount,&flast,&flastsolid);
        if  (result == 1)
        {
            DoPCInt(PC_COMM_USER_BUTTON,2);
            DoPCInt(PC_COMM_RESTART);
        }

Thanks,

Scott


Group: DynoMotion Message: 11580 From: Tom Kerekes Date: 5/26/2015
Subject: Re: External Buttons
Hi Scott,

Different state variables are needed to maintain the state for each button that you have.  These variables are used by the Debounce function to remember what the last state was for that button, how long it has been pushed, etc. 

Declare 3 new variables and pass those to the other routine.  To make the variable names different I use a different first letter so instead of fcount flast flastsolid

use

bcount blast  blastsolid

HTH
Regards
TK
Group: DynoMotion Message: 11587 From: cnc_machines Date: 5/27/2015
Subject: Re: External Buttons
Thank you for your patience Tom, I am slowly getting up to speed. Your help has really allowed me to learn quickly. Much appreciated.